﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.Composition;
using System.Data;
using com.nm;
using com.nm.library.Excels;

namespace DistributeAPI.Export
{
    /// <summary>
    /// 企划订单量计算Excel导入
    /// </summary>
    [Export(typeof(ExcelImport))]
    public class NewPlanningOrderQuantityExcel : ExcelImport
    {
        private static int num = 0;
        public override ExcelImportType Type => ExcelImportType.NEWPLANNINGORDERQUANTITY;

        public override Dictionary<string, ImportVerify> DictFields => dictFields;

        /// <summary>
        /// Excel字段映射
        /// </summary>
        private static readonly Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
        new ImportVerify{ ColumnName="店铺编号",    FieldName="ShopCode" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,true,false)},
            new ImportVerify{ ColumnName="店铺名称",    FieldName="ShopName" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,false,false)},
            new ImportVerify{ ColumnName="货号",    FieldName="MaterialID" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,true,false)},
            new ImportVerify{ ColumnName="尺码长度",    FieldName="SizeLength" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,true,false)},
            new ImportVerify{ ColumnName="数量",    FieldName="TotalQty" ,DataType=typeof(long),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,false)},
           }.ToDictionary(e => e.ColumnName, e => e);

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var handler = new NewPlanOrderQuantityHandler();
            var uid = Guid.NewGuid().ToString().Replace("-", "");
            var tempTableName = $@"ngoods.temp{uid}";
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            {
                handler.DropTempTable(tempTableName);
                handler.CreateTempTable(dt, tempTableName);
                var errorDT = handler.ImportPlanningOrderQuantity(tempTableName);
                result.Message =
                    $"成功导入{(dt.Rows.Count - errorDT.Rows.Count)}条数据{(errorDT.Rows.Count == 0 ? "" : $@",有{errorDT.Rows.Count}条错误数据")}";
                if (errorDT.Rows.Count != 0)
                {
                    var path = ExcelImporMapper.GetTemplatePath(Type);
                    result.Url = ExcelUtil.ExportErrorExeclToDB(errorDT, path, dictFields, "企划订单量错误数据", num);
                    num++;
                }
            }
            catch (Exception ex)
            {
                Log.Error("导入企划订单量计算：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            finally
            {
                handler.DropTempTable(tempTableName);
            }
            return result;
        }

        public override DataTable GetExportData(string Keys)
        {
            throw new NotImplementedException();
        }
    }
}

